package com.qkun.library.utils;

import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CheckRomUtils {

    private static final String TAG = "CheckRomUtils";

    private static final String ROM_MIUI = "MIUI";//小米
    private static final String ROM_EMUI = "EMUI";//华为
    private static final String ROM_FLYME = "FLYME";//魅族
    private static final String ROM_OPPO = "OPPO";//oppo
    private static final String ROM_SMARTISAN = "SMARTISAN";//锤子
    private static final String ROM_VIVO = "VIVO";//vivo
    private static final String ROM_QIKU = "QIKU";//360
    private static final String ROM_360 = "360";//360

    private static final String KEY_VERSION_MIUI = "ro.miui.ui.version.name";
    private static final String KEY_VERSION_EMUI = "ro.build.version.emui";
    private static final String KEY_VERSION_OPPO = "ro.build.version.opporom";
    private static final String KEY_VERSION_SMARTISAN = "ro.smartisan.version";
    private static final String KEY_VERSION_VIVO = "ro.vivo.os.version";

    private static String sName;
    private static String sVersion;

    public static boolean isEmui() {
        return check(ROM_EMUI);
    }

    public static boolean isMiui() {
        return check(ROM_MIUI);
    }

    public static boolean isVivo() {
        return check(ROM_VIVO);
    }

    public static boolean isOppo() {
        return check(ROM_OPPO);
    }

    public static boolean isFlyme() {
        return check(ROM_FLYME);
    }

    public static boolean is360() {
        return check(ROM_QIKU) || check(ROM_360);
    }

    public static boolean isSmartisan() {
        return check(ROM_SMARTISAN);
    }

    public static boolean isFromChina() {
        return isEmui()
                || isMiui()
                || is360()
                || isFlyme()
                || isOppo()
                || isVivo()
                || isSmartisan();
    }

    public static String getName() {
        if (TextUtils.isEmpty(sName)) check("");
        return sName;
    }

    public static String getVersion() {
        if (TextUtils.isEmpty(sVersion)) check("");
        return sVersion;
    }

    public static boolean check(String rom) {
        if (!TextUtils.isEmpty(sName)) return sName.equals(rom);
        if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_MIUI))) sName = ROM_MIUI;
        else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_EMUI))) sName = ROM_EMUI;
        else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_OPPO))) sName = ROM_OPPO;
        else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_VIVO))) sName = ROM_VIVO;
        else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_SMARTISAN)))
            sName = ROM_SMARTISAN;
        else {
            sVersion = Build.DISPLAY;
            if (sVersion.toUpperCase().contains(ROM_FLYME)) sName = ROM_FLYME;
            else {
                sVersion = Build.UNKNOWN;
                sName = Build.MANUFACTURER.toUpperCase();
            }
        }
        return sName.equals(rom);
    }

    public static String getProp(String name) {
        String line = null;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + name);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "Unable to read prop " + name, e);
        } finally {
            if (Utils.nonNull(input)) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return line;
    }

}
